package com.gm.commonlib.network;

import com.gm.commonlib.application.MyApplicationHolder;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.network.basenetwork.BaseHttpClient;

import org.reactivestreams.Subscription;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import retrofit2.Retrofit;

public class HttpClient {
    private static Retrofit sRetrofit;

    public static void destory() {
        sRetrofit = null;
    }

    public static <T> T createService(Class<T> service) {
        sRetrofit = BaseHttpClient.getHttpclient().getRetrofitInstanse();
        return sRetrofit.create(service);
    }

    public static Retrofit getRetrofitInstance() {
        sRetrofit = BaseHttpClient.getHttpclient().getRetrofitInstanse();
        return sRetrofit;
    }


    public static void reset() {
        BaseHttpClient.getHttpclient().reset();
    }

    public static <T> Subscription sendRequest(final Observable<T> observable, final BaseHttpObserver<T> observer) {
        return sendRequest(null, observable, observer, false, true);
    }

    public static <T> Subscription sendRequest(IBaseView iBaseView, final Observable<T> observable, final BaseHttpObserver<T> observer) {
        return sendRequest(iBaseView, observable, observer, false, true);
    }


    public static <T> Subscription sendRequest(IBaseView iBaseView, final Observable<T> observable,
                                               final BaseHttpObserver<T> observer, boolean isRefresh, boolean reloginFlag) {
        if (observer != null) {
            observer.setRefresh(isRefresh);
            observer.setBaseView(iBaseView);
            observer.showLoadingDialog();
        }
        BaseHttpClient.getHttpclient().sendRequest(observable, new ObserverImpl<T>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (observer != null) {
                            observer.onSubscribe(d);
                        }
                    }

                    @Override
                    public void onNext(T t) {
                        if (t instanceof BaseResp) {
                            BaseResp baseResp = (BaseResp) t;
                            //todo token失效 重新登录
                            int code=baseResp.getCode();
                            if (code==HttpResultCode.TOKEN_ERROR.getCode()
                            || code==HttpResultCode.USER_NOT_EXIT.getCode()
                            || code==HttpResultCode.TENANT_NOT_EXIT.getCode()){
                               MyApplicationHolder.getApplication().login(baseResp.getCode());
                                return;
                          }
                            observer.onNext(t);
                        } else {
                            if (observer != null) {
                                observer.onNext(t);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (observer != null) {
                            observer.onError(e);
                        }
                    }

                    @Override
                    public void onComplete() {
                        if (observer != null) {
                            observer.onComplete();
                        }
                    }

                }
        );
        return null;
    }

    /**
     * 发送请求请求
     *
     * @param holder
     * @param observer
     * @param <T1>
     * @param <T2>
     * @return
     */
    public static <T1, T2> Subscription sendRequest(IBaseView iBaseView, HttpServicesHolder<T1, T2> holder,
                                                    BaseHttpObserver<T2> observer) {
        return sendRequest(iBaseView, holder, observer, false, true);
    }

    public static <T1, T2> Subscription sendRequest(IBaseView iBaseView, boolean isRefresh, HttpServicesHolder<T1, T2> holder,
                                                    BaseHttpObserver<T2> observer, boolean reloginFlag) {
        return sendRequest(iBaseView, holder, observer, isRefresh, reloginFlag);
    }

    public static <T1, T2> Subscription sendRequest(IBaseView iBaseView, boolean isRefresh, HttpServicesHolder<T1, T2> holder,
                                                    BaseHttpObserver<T2> observer) {
        return sendRequest(iBaseView, holder, observer, isRefresh, true);
    }

    public static <T1, T2> Subscription sendRequest(IBaseView iBaseView, HttpServicesHolder<T1, T2> holder,
                                                    BaseHttpObserver<T2> observer, boolean reloginFlag) {
        return sendRequest(iBaseView, holder, observer, false, reloginFlag);
    }

    /**
     * 发送网络请求
     *
     * @param holder      {@link HttpServicesHolder}用于保存各个不同的请求实体，并在其子类实现特定的请求方法调用。
     * @param observer    {@link BaseHttpObserver}抽象的网络访问观察者，用于处理网络请求回调。
     * @param isRefresh   一般用于列表，标识是否刷新
     * @param reloginFlag 是否在发送请求前根据过期时间判断是否重新登录
     * @param <T1>        网络请求接口
     * @param <T2>        返回的结果实体类
     * @return {@link Subscription} 用于取消网络请求
     */
    public static <T1, T2> Subscription sendRequest(IBaseView iBaseView, final HttpServicesHolder<T1, T2> holder,
                                                    final BaseHttpObserver<T2> observer,
                                                    boolean isRefresh, boolean reloginFlag) {

        if (holder == null) {
            return null;
        }

        final Observable<T2> tHttpObservable = holder.getHttpObservable();
        if (tHttpObservable == null) {
            return null;
        }
        return sendRequest(iBaseView, tHttpObservable, observer, isRefresh, reloginFlag);
    }


}
